<!-- 房屋管理 -->
<template>
  <div>
    <div class="header">
      <div class="title" style="height: 65px">
        <div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px">
          <a-space style="margin-left: 10px">
            <span>核验开始时间：</span>
            <a-date-picker v-model:value="startDate" :format="dateFormat" :disabledDate="disabledFutureDate"  />
          </a-space>
          <a-space style="margin-left: 10px">
            <span>核验结束时间：</span>
            <a-date-picker v-model:value="endDate" :format="dateFormat" :disabledDate="disabledFutureDate"  />
          </a-space>
          <a-space style="margin-top: 5px; margin-left: 10px">
            <a-select ref="select" v-model:value="hyzt" style="width: 150px"   v-if="$route.query.showSelect === 'true'">
              <a-select-option value="">核验状态</a-select-option>
              <a-select-option value="已核验">已核验</a-select-option>
              <a-select-option value="未核验">未核验</a-select-option>
            </a-select>
          </a-space>
          <a-space style="margin-top: 15px; margin-left: 15px">
            <span>核验人员姓名：</span>
            <a-input
              v-model:value="nickname"
              placeholder="请输入核查人员姓名进行查询"
              style="width: 200px; margin-top: -10px"
            />
          </a-space>
          <a-space style="margin-left: 10px; margin-top: 15px">
            <span>关键字：</span>
            <a-input
              v-model:value="txtKeyWord"
              placeholder="请输入姓名、地址或房屋编号进行查询"
              style="width: 300px; margin-top: -10px"
            />
          </a-space>
          &nbsp;&nbsp;&nbsp;&nbsp;
          <a-space class="search-btn">
            <a-button type="primary" @click="searchBtn">查询</a-button>
          </a-space>
        </div>
      </div>
      <div style="width: 100%; background: #fff; height: 40px">
        <div style="width: 45%; float: left; font-size: 16px; padding-left: 20px; line-height: 37px"
          ><home-outlined two-tone-color="#eb2f96" /><span style="padding: 0px 10px">
            {{ title }}</span
          ></div
        >
        <div
          style="
            width: 30%;
            float: right;
            font-size: 16px;
            padding-left: 20px;
            line-height: 37px;
            text-align: right;
            padding-right: 20px;
          "
          ><a style="color: blue" @click="getBack"
            ><arrow-left-outlined two-tone-color="#eb2f96" style="padding: 0px 10px" />返回上一页</a
          ></div
        >
      </div>
      <a-table
        style="margin-top: 0px"
        :dataSource="tableData"
        :columns="displayColumns"
        :pagination="false"
        :scroll="{ x: 1500, y: tableHeight }"
        bordered
        :loading="loading"
      />
      <div class="pagination" style="float: right">
        <div style="float: left; line-height: 32px; padding-right: 12px">共{{ total }}条记录</div>
        <a-pagination
          show-size-changer
          show-total
          v-model:current="pageIndex"
          v-model:pageSize="pageSize"
          :total="total"
          style="float: right"
          @show-size-change="onShowSizeChange"
        />
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
  import moment from 'moment';
  import dayjs from 'dayjs';
  import { ref, reactive, toRefs, onMounted, onBeforeMount, computed, watch } from 'vue';
  import { HomeOutlined, ArrowLeftOutlined } from '@ant-design/icons-vue';
  import { useRouter, useRoute } from 'vue-router';
  import { getTaskHouseList } from '/@/api/demo/api'; //获取房屋列表
  import { getRegion, delethouseApi } from '/@/api/demo/system'; //统计工作进展
  import { useUserStore } from '/@/store/modules/user';
  import { iTableData, iTableColumns } from './types/table';
  import { iParams } from './types/params';
  import { useCheckStore } from '/@/store/modules/checkStore';
  let userInfo = ref<any>(null);
   const disabledFutureDate = (current) => {
  return current && current > moment().endOf('day');
};
  let options = ref<any[]>([]);
  const title = ref<string>(''); // 开始时间
  let detailTableHeight = ref<number>(0); // 表格高度
  let tableHeight = ref<number>(0); // 表格高度
  let pageIndex = ref(1);
  let pageSize = ref(15);
  const dateFormat = 'YYYY-MM-DD';
  const startDate = ref<any>(null); // 开始时间
  const endDate = ref<any>(null); // 结束时间
  const value18 = ref<string[]>([]);
  let txtKeyWord = ref<string>('');
  let nickname = ref<string>('');
  let hyzt = ref<string>('');
  let total = ref(0);
  const store = useCheckStore();
  const { taskid,  currentTitle,hyzt2,villagedm,taskname,village,jsrysstd } = store.formData || {};
  // 使用 useRouter 创建一个 router 实例
  const router = useRouter();
  let data = reactive<{
    tableData: iTableData[];
    tableColumns: iTableColumns[];
    wscolsList: { wch: number }[];
    id: number | any;
    loading: Boolean;
    keyWorld: String;
    currentqx: string; // 当前行政区名称
    currentTitle: string;
    cityToken: string | any;
  }>({
    // 表格数据
    tableData: [],
    tableColumns: [
      {
        title: '序号',
        dataIndex: 'index',
        key: 'index',
        align: 'center',
        width: 80,
      },
     {
        title: '房屋编号',
        dataIndex: 'fwbh',
        key: 'fwbh',
        align: 'center',
        width: 180,
        customCell: (row) => {
          return {
            innerHTML: `<span style='color:blue;cursor:pointer;text-decoration:underline' >${row.fwbh}</span>`,
            onClick: async () => {
              getDetailData(row);
            },
          };
        },
      },
      {
        title: '层数',
        dataIndex: 'cs',
        key: 'cs',
        align: 'center',
        width: 100,
      },
      {
        title: '是否满足“双通道”',
        dataIndex: 'jsrysstd',
        key: 'jsrysstd',
        align: 'center',
        width: 180,
      },
      {
        title: '核验人员',
        dataIndex: 'nickname',
        key: 'nickname',
        align: 'center',
        width: 200,
      },
      {
        title: '核验状态',
        dataIndex: 'fwzt',
        key: 'fwzt',
        align: 'center',
        width: 100,
        customCell: (row) => {
          let style = '';
          let yhztName = '';
          if (row.hyzt == '1') {
            style = 'color: green;';
            yhztName = '已核验';
          } else {
            style = 'color: red;';
            yhztName = '未核验';
          }
          return {
            style: style,
            innerHTML: `<span>${yhztName}</span>`,
            class: 'cell-hover',
          };
        },
      },
      
      {
        title: '核验时间',
        dataIndex: 'yh_create_on',
        key: 'yh_create_on',
        align: 'center',
        width: 200,
      },
      {
        title: '户主',
        dataIndex: 'hzxm',
        key: 'hzxm',
        align: 'center',
        width: 100,
      },
      {
        title: '地址',
        dataIndex: 'address',
        key: 'address',
        align: 'center',
        width: 280,
      },  
      {
        title: '街/乡镇名称',
        dataIndex: 'town',
        key: 'town',
        align: 'center',
        width: 120,
      },   
      {
        title: '村/社区名称',
        dataIndex: 'village',
        key: 'village',
        align: 'center',
        width: 150,
      },
       {
        title: '区名称',
        dataIndex: 'xzq',
        key: 'xzq',
        align: 'center',
        width: 80,
      },
      
    ],
    id: null,
    loading: true,
    keyWorld: '',
    cityToken: '', // 判断是不是市区 110000 是北京市 展示搜索宽
    currentqx: '', // 当前行政区名称
    currentTitle: '', // 当前详情标题
    wscolsList: [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }],
  });
  // 表格的 ref Dom名称
  let { tableData, loading } = toRefs(data);

  async function getData() {
    let params: iParams = {
      pageIndex: pageIndex.value,
      pageSize: pageSize.value,
      taskid: taskid + '',
      hyzt: hyzt2 || hyzt.value,
      txtKeyWord: txtKeyWord.value,
      nickname: nickname.value,
      villagedm: villagedm || '',
      jsrysstd: jsrysstd ||  ''
    };
    if (startDate.value != null) {
      params.startTime = moment(startDate.value.$d).format('YYYY-MM-DD');
      params.endTime = moment(endDate.value.$d).format('YYYY-MM-DD');
    }
    console.log('*****', params);
    loading.value = true;
    let res: any = await getTaskHouseList(params);
    console.log('@res--工作进入汇总表', res.items);

    if (res || res.items.length > 0) {
      loading.value = false;
      data.tableData = [...res.items];
      console.log('@res--工作进入汇总表', tableData);
      total.value = res.total;
      if(hyzt2=="已核验")
      {
         title.value = '【' +taskname+'】任务下'+ '【' +village+'】'+'(已核验栋数：' + res.total + '栋)';
      }
      else  if(hyzt2=="未核验")
      {
         title.value = '【' +taskname+'】任务下'+ '【' +village+'】'+'(未核验栋数：' + res.total + '栋)';
      }
      else if(jsrysstd=="是")
      {
         title.value = '【' +taskname+'】任务下'+ '【' +village+'】'+'(已达标栋数：' + res.total + '栋)';
      }
      else  if(jsrysstd=="否")
      {
         title.value = '【' +taskname+'】任务下'+ '【' +village+'】'+'(未达标栋数：' + res.total + '栋)';
      }
      else
      {
      title.value = '【' +taskname+'】任务下'+ '【' +village+'】'+'核验情况(总栋数：' + res.total + '栋)';
      }
    }
  }

  function getUserInfo() {
    const userStore = useUserStore();
    userInfo.value = userStore?.getUserInfo;
  }

  function searchBtn() {
    data.loading = true;
   // value18.value = [];
    getData();
  }

  async function getRegionData() {
    let res: any = await getRegion();
    options.value = res;
  }
  function onChange(value) {
    value18.value = value;
  }
  const displayColumns = computed(() => {
    const { role } = userInfo.value;
    let arr: any[] = [];
    if (role === 'City') {
      // qx
      arr = data.tableColumns.filter((item) => item.key !== 'jzmc' && item.key !== 'czmc');
      return arr;
    } else if (role === 'Area') {
      // jz
      arr = data.tableColumns.filter((item) => item.key !== 'czmc' && item.key !== 'qxmc');
      return arr;
    } else if (role === 'Street') {
      // cun
      arr = data.tableColumns.filter((item) => item.key !== 'jzmc' && item.key !== 'qxmc');
      console.log('@Aarr', arr);
      return arr;
    } else {
      return data.tableColumns;
    }
  });
  //跳转记录页面
  function getDetailData(row: any) {
    router.push({
      path: '/utils/houseCheckLog',
      query: {
        xzq: row.xzq,
        town: row.town,
        village: row.village,
        hzxm: row.hzxm,
        fwbh: row.fwbh,
      },
    });
  }
  const getBack = () => {
    router.go(-1);
  };
  const onShowSizeChange = () => {
  };
  const getTableHeight = () => {
    setTimeout(() => {
      detailTableHeight.value = window.innerHeight;
      tableHeight.value = window.innerHeight;
    });
  };
  watch(pageSize, () => {
    console.log('pageSize', pageSize.value);
    getData();
  });
  watch(pageIndex, () => {
    console.log('current', pageIndex.value);
    getData();
  });
  onBeforeMount(() => {
    getUserInfo();
  });
  onMounted(() => {
    console.log('******工作进展统计初始化******');
    getRegionData();
    getData();
    getTableHeight();
    // getTableData();
  });
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
  #map {
    height: calc(100vh - 140px);
  }
  .col-sql {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .x-houseMng-search {
    z-index: 100;
    margin: 6px;
    position: absolute;
  }

  .root-house-mng-main {
    margin: 6px;
  }

  .x-village-tabs {
    //z-index: 100;
    //position: absolute;
    width: 60vw;
    margin-top: 6px;
    margin-left: 40px;
    //background-color: rgba(34, 34, 34, 0.6);
    :deep(.ant-form) {
      margin-top: 6px;
    }

    // :deep(.ant-form-item-label) > label {
    //   color: white;
    // }
  }
</style>
